//
//  STBannerCollectionViewController
//  iPhone
//
//  Created by Toj on 5/24/23.
//

import UIKit

// MARK: - 懒加载(属性), 变量, 初始化init
class STBannerCollectionViewController: STBaseViewController {
    
    private let viewModel = BannerCollectionViewModel()
    
    /* management */
    private lazy var management: UICollectionViewManagement = {
        let view = UICollectionViewManagement(viewModel,cell: self)
        return view
    }()
    private lazy var collectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .vertical
        layout.minimumLineSpacing = 8
        layout.minimumInteritemSpacing = 0
        
        let view = UICollectionView(frame: .zero, collectionViewLayout: layout)
        view.scrollsToTop = true
        view.showsVerticalScrollIndicator = false
        view.showsHorizontalScrollIndicator = false
        view.backgroundColor = .white
        view.register(cellClass: MoviesCellModel.registerCell)
        view.register(cellClass: BannerAdCellModel.registerCell)
        view.dataSource = management
        view.delegate = management
        return view
    }()
    
    deinit {
        print("-->toj: class: \(cm_objectName), func: \(#function)")
    }
}

// MARK: - 生命周期
extension STBannerCollectionViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupMakeInitialize()
        setupMakeAddSubviews()
        setupMakeLayoutSubviews()
        
        collectionView.reloadData()
    }
}

// MARK: - 点击事件
@objc private extension STBannerCollectionViewController {
    
}

// MARK: - Private
private extension STBannerCollectionViewController {
    
}

extension STBannerCollectionViewController: UICollectionViewCellDelegate {
    func collectionViewReloadData() {
        CATransaction.begin()
        CATransaction.setDisableActions(true)
        collectionView.reloadData()
        CATransaction.commit()
    }
    
    func collectionViewReloadCell(_ cell: UICollectionViewCell) {
        guard let indexPath = collectionView.indexPath(for: cell) else { return }
        CATransaction.begin()
        CATransaction.setDisableActions(true)
        collectionView.reloadItems(at: [indexPath])
        CATransaction.commit()
    }
    
    func collectionViewDidSelected(at indexPath: IndexPath) {
        CATransaction.begin()
        CATransaction.setDisableActions(true)
        collectionView.reloadItems(at: [indexPath])
        CATransaction.commit()
    }
    
    func collectionViewInvalidateLayout() {
        CATransaction.begin()
        CATransaction.setDisableActions(true)
        collectionView.collectionViewLayout.invalidateLayout()
        CATransaction.commit()
    }
}

// MARK: - 初始化设置
private extension STBannerCollectionViewController {
    
    func setupMakeAddSubviews() {
        
        view.addSubview(collectionView)
    }
    
    func setupMakeLayoutSubviews() {
        collectionView.snp.makeConstraints { make in
            make.left.right.bottom.equalTo(view)
            make.top.equalTo(CGFloat.nav_barFullHeight)
        }
    }
    
    func setupMakeInitialize() {
        viewModel.requestBannerAds()
    }
}
